package Demo32;

import java.util.HashMap;

/**
 * 连续数组
 * https://leetcode.cn/problems/contiguous-array/
 */

class Solution {
    public int findMaxLength(int[] nums) {
        HashMap<Integer,Integer> hash = new HashMap<>();
        hash.put(0,-1);
        int sum = 0,ret = 0;
        for(int i = 0; i < nums.length;i++) {
            sum += (nums[i] == 0? -1:1);
            if(hash.containsKey(sum)) ret = Math.max(ret,i - hash.get(sum));
            else hash.put(sum,i);
        }
        return ret;
    }
}
class Demo {
    public static void main(String[] args) {
        Solution solution = new Solution();
        int ret = solution.findMaxLength(new int[] {0,0,0,1,1,1,0});
        System.out.println(ret);
    }
}